package com.luajit.decompile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class TestLaya {
    public static  void  main(String args[]){

        findFolderFileList("cache");
        System.out.println(map.keySet());

        TreeMap<Integer, String> treeMap = new TreeMap();
        for (String k : map.keySet()         ) {
            treeMap.put(map.get(k).size(),k);
        }
        System.out.println(treeMap.keySet());
    }

    static Map<String, List<String>> map = new HashMap();



    /**
     *
     * @param path
     */
    public static void findFolderFileList(String path) {
        File file1 = new File(path);
        if (file1.exists()) {
            File[] files = file1.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println(file2.getAbsolutePath());
                        findFolderFileList(file2.getAbsolutePath());
                    } else {
                        try {

                            FileInputStream in = new FileInputStream(file2);
                            int tempbyte;
                            //1b4c4a12
                            StringBuilder sb = new StringBuilder();
                            int[] header = new int[6];
                            for (int i = 0; i < 6; i++) {
                                tempbyte = in.read();
                                header[i] = tempbyte;
                                sb.append((char)header[i]) ;
                            }

                            in.close();

                            if (map.get(sb.toString())==null){
                                map.put(sb.toString(),new ArrayList<String>());
                                map.get(sb.toString()).add(file2.getName());
                            }else {
                                map.get(sb.toString()).add(file2.getName());
                            }


                            //ab 4b 54 58 ktx  89 50 4e 47 png ff d8 ff e0 jpeg
                            if (header[0]==0x89&&header[1]==0x50&&header[2]==0x4e&&header[3]==0x47   ){
                                String pathName =  file2.getAbsolutePath();
                                File test = new File(file2.getName()+".png");
                                FileInputStream testin = new FileInputStream(file2);
                                FileOutputStream testout = new FileOutputStream(test);

                                byte[] buffer = new byte[1024];
                                int len;

                                while ((len = testin.read(buffer)) > 0) {
                                    testout.write(buffer, 0, len);
                                }
                                testin.close();
                                testout.close();
                                System.out.println("found png");
                            }
                            else if (header[0]==0xff&&header[1]==0xd8&&header[2]==0xff&&header[3]==0xe0   ){
                                String pathName =  file2.getAbsolutePath();
                                File test = new File(file2.getName()+".jpeg");
                                FileInputStream testin = new FileInputStream(file2);
                                FileOutputStream testout = new FileOutputStream(test);

                                byte[] buffer = new byte[1024];
                                int len;

                                while ((len = testin.read(buffer)) > 0) {
                                    testout.write(buffer, 0, len);
                                }
                                testin.close();
                                testout.close();
                                System.out.println("found jpeg");
                            }
                            //ab 4b 54 58 ktx
                            else   if (header[0]==0xab&&header[1]==0x4b&&header[2]==0x54&&header[3]==0x58   ) {

                                String pathName =  file2.getAbsolutePath();
                                File test = new File("test/"+file2.getName()+".ktx");
                                FileInputStream testin = new FileInputStream(file2);
                                FileOutputStream testout = new FileOutputStream(test);

                                byte[] buffer = new byte[1024];
                                int len;

                                while ((len = testin.read(buffer)) > 0) {
                                    testout.write(buffer, 0, len);
                                }
                                testin.close();
                                testout.close();



                                String fname = file2.getName() ;
                               //D:\AndroidProjects\PluginAppMain\out> PVRTexToolCLI -i a0095bba.ktx -d -f r8g8b8a8
                                // Process p = Runtime.getRuntime().exec("cmd /c D: & cd D:/AndroidProjects/PluginAppMain/out & PVRTexToolCLI -i "+fname+" -d -f r8g8b8a8");
                                System.out.println("PVRTexToolCLI -i "+fname+".ktx -d -f r8g8b8a8");
                             //   Thread.sleep(1*5000);
                            }

                        }catch (Exception e){
                            System.out.println("error!!!"+e.getMessage());
                        }

                    }
                }
            }
        } else {
            System.out.println("error!!!");
        }
    }
}
